﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;

namespace DRHotel_v1.UserAccountAndMultiCreation
{
    public partial class UserAccountAndMultiCreationForm : Form
    {
        public UserAccountAndMultiCreationForm()
        {
            InitializeComponent();
        }

        private void UserAccountAndMultiCreationForm_Load(object sender, EventArgs e)
        {
           
            // TODO: This line of code loads data into the 'dRHotelMngSysDataSet.Account' table. You can move, or remove it, as needed.
            this.accountTableAdapter.Fill(this.dRHotelMngSysDataSet.Account);
        
            ArrayList staffList = AccountCreationDBManager.PopulateStaff();
            for (int i = 0; i < staffList.Count; i++)
            {
                StaffDetailClass sta = (StaffDetailClass)staffList[i];
                cbxStaffID.Items.Add(sta.staffID);
            }
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
              
                string staffID = cbxStaffID.SelectedItem.ToString();
                string username = tbxUsername.Text;
                string password = tbxPassword.Text;
                string RetypePassword = tbxRetypePassword.Text;
                string usertype = cbxUsertype.SelectedItem.ToString();

                if (password != RetypePassword)
                {
                    MessageBox.Show("Password are not the same, please try again. ");
                }
                else
                {
                    AccountCreationDBManager acc = new AccountCreationDBManager();
                    bool accountExist = acc.AccountExist(staffID, username);

                    if (accountExist == true)
                    {
                        MessageBox.Show("Account already exists. Please choose a different Staff ID.");
                    }
                    else
                    {
                        bool status = acc.CreateAccount(staffID, username, password, usertype);
                        if (status == true)
                        {
                            MessageBox.Show("Account created successfully");
                        }
                        else
                        {
                            MessageBox.Show("Not created, please try again!");
                        }
                    }


                }
            }
            catch (NullReferenceException nre)
            {
                string errorMsg = "";
                if (cbxStaffID.SelectedIndex == -1)
                {
                    errorMsg += "Please select a Staff ID\n";
                }
                if (tbxUsername.Text.Length == 0)
                {
                    errorMsg += "Please fill in your username\n";
                }
                if (tbxPassword.Text.Length == 0)
                {
                    errorMsg += "Please fill in your password\n";
                }
                if (tbxRetypePassword.Text.Length == 0)
                {
                    errorMsg += "Please fill in your confirm password.\n";
                }
                if (cbxUsertype.SelectedIndex == -1)
                {
                    errorMsg += "Please select a user type.";
                }
                MessageBox.Show(errorMsg);
            }

        }

        private void cbxStaffID_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = cbxStaffID.SelectedIndex;
            ArrayList staffList = AccountCreationDBManager.PopulateStaff();
            StaffDetailClass sta = (StaffDetailClass)staffList[index];
            tbxFirstname.Text = sta.firstName;
            txbLastname.Text = sta.lastName;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void bindingSource1_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.accountTableAdapter.Fill(this.dRHotelMngSysDataSet.Account);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            
            try
            {
                accountTableAdapter.Update(this.dRHotelMngSysDataSet.Account);
                MessageBox.Show("Updated Successfully");
            }
            catch (SqlException sqlex)
            {
                MessageBox.Show(sqlex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            dataGridView1.Enabled = true;
        }

        private void fillToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.accountTableAdapter.Fill(this.dRHotelMngSysDataSet.Account);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void fillToolStripButton_Click_1(object sender, EventArgs e)
        {
            try
            {
                this.accountTableAdapter.Fill(this.dRHotelMngSysDataSet.Account);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void fillToolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                this.accountTableAdapter.Fill(this.dRHotelMngSysDataSet.Account);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

      

       

       

      

       

      

        

    }
}
